package cn.ekuma.pos.pda.sale.table;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import cn.ekuma.android.keyBoardinput.KeyBoradInputPanel;
import cn.ekuma.android.ui.scan.CaptureScanActivity;
import cn.ekuma.data.ui.android.OnBaseBeanSelectedListener;
import cn.ekuma.epos.android.finder.AdvProductSelectActivate;
import cn.ekuma.epos.android.finder.CustomerFinderActivate;
import cn.ekuma.epos.android.payment.PDAPaymentSelect;
import cn.ekuma.epos.android.print.DeviceTicket;
import cn.ekuma.epos.android.print.TicketParser;
import cn.ekuma.epos.android.util.AnimationController;
import cn.ekuma.epos.bean.util.sale.TicketHelper;
import cn.ekuma.pos.android.pos.R;
import cn.ekuma.pos.pda.finder.ShardTicketFinder;
import cn.ekuma.pos.pda.sale.AbstractTicktSaleActivity;
import cn.ekuma.pos.pda.sale.TicketLineListAdapter;
import cn.ekuma.pos.pda.sale.TicketLines;

import com.openbravo.bean.Product;
import com.openbravo.bean.sales.Ticket;
import com.openbravo.bean.sales.TicketLine;

public class TicketSaleTableActivity extends AbstractTicktSaleActivity {

	public static String TICKET_TO_SHOW="ticketToShow";
	
	public static final int PAYMENT_TICKET=RETURN_TYPE_TOTAL+1;

	protected ImageButton btnSelectShardTicket,btnSelectTable,btnBarcode,btnSelectProduct,btn_adv_newticket,btn_adv_clearticket;
	protected Button btnTicketTitle;

	
	protected KeyBoradInputPanel keyInportPanel;
	
	private TicketSaleTableProductsPanel productListPanel;
	
	protected TicketParser m_TTP;
	
	protected ImageButton ctrl_moveUp,ctrl_moveDown,ticket_table_cash;
	AnimationController animControl;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.table_pos);
		lineAdapter=new TicketLineListAdapter(this);
		ticketList=new TicketLines(this,ticketManager,lineAdapter);
		
		ViewGroup contentPanel=(ViewGroup) this.findViewById(R.id.content_panel);
		contentPanel.addView(ticketList);
		
		animControl=new AnimationController();

		btnTicketTitle=(Button) findViewById(R.id.btnTicketTitle);
		btnSelectShardTicket=(ImageButton) findViewById(R.id.btnSelectShardTicket);
		btnSelectTable=(ImageButton) findViewById(R.id.btnSelectTable);
		btnSelectProduct=(ImageButton) findViewById(R.id.btnSelectProduct);
		btnBarcode=(ImageButton) findViewById(R.id.btnBarcode);
		
		btn_adv_newticket=(ImageButton) findViewById(R.id.btn_adv_newticket);
		btn_adv_clearticket=(ImageButton) findViewById(R.id.btn_adv_clearticket);
		
		btnSelectShardTicket.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent();
		    	intent.setClass(TicketSaleTableActivity.this, ShardTicketFinder.class);
		    	startActivityForResult(intent,SELECT_TICKET);
			}
			
		});
		keyInportPanel=(KeyBoradInputPanel) findViewById(R.id.ticket_table_key_import);
		final View ticket_state=findViewById(R.id.ticket_state);
		
		productListPanel=(TicketSaleTableProductsPanel) findViewById(R.id.ticketSaleTableProductsPanel1);
		productListPanel.setOnBaseBeanSelectedListener(new OnBaseBeanSelectedListener<Product>(){
			public void baseBeanSelected(Product obj) {
				ticketManager.addSelectedProduct(obj);
			}
		});
		
		productListPanel.setZoomClickListener(new OnClickListener(){

			public void onClick(View v) {
				if(ticket_state.getVisibility()==View.VISIBLE)
					   animControl.scaleOut(ticket_state, 100, 100);
					else
						animControl.scaleIn(ticket_state, 100, 100);	
			}	
		});
		
		 getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
		            WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

			btnBarcode.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					Intent intent = new Intent();
			    	intent.setClass(TicketSaleTableActivity.this, CaptureScanActivity.class);
			    	startActivityForResult(intent,SCAN_PRODUCTS);
				}
			});
			
			
			btnSelectProduct.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					Intent intent = new Intent();
			    	intent.setClass(TicketSaleTableActivity.this, AdvProductSelectActivate.class);
			    	startActivityForResult(intent,SELECT_PRODUCTS);
				}
			});
			
			btnTicketTitle.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					Intent intent = new Intent();
			    	intent.setClass(TicketSaleTableActivity.this, CustomerFinderActivate.class);
			    	startActivityForResult(intent,SELECT_CUSTOMER);
				}
			});
			
			
			btn_adv_newticket.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					ticketManager.newTicket();
				}
			});
			
			btn_adv_clearticket.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					ticketManager.clearCurrentTicket();
				}
			});
			
			ticketList.setOnBaseBeanSelectedListener(this);
			keyInportPanel.setInputListener(this);
			keyInportPanel.setStateChangeListener(new KeyBoradInputPanel.ViewStateChangeListener(){
				public void onExpand(boolean isExpand){
					findViewById(R.id.advButtoms).setVisibility(isExpand?View.VISIBLE:View.GONE);  
					findViewById(R.id.ticketSaleTableCustomerPanel1).setVisibility(isExpand?View.VISIBLE:View.GONE);
				}
			});
			
			
			m_TTP=new TicketParser(new DeviceTicket());
			
			init();
			productListPanel.onLoadData(this.ticketHandler);
					
			ctrl_moveUp=(ImageButton)findViewById(R.id.img_ctl_moveup);	
			ctrl_moveUp.setOnClickListener(new OnClickListener(){

				public void onClick(View v) {
					ticketList.moveUp();
				}
				
			});
			ctrl_moveDown=(ImageButton)findViewById(R.id.img_ctl_movedown);	
			ctrl_moveDown.setOnClickListener(new OnClickListener(){

				public void onClick(View v) {
					ticketList.moveDown();
				}
				
			});
			
			ticket_table_cash=(ImageButton)findViewById(R.id.btm_ticket_table_cash);	
			ticket_table_cash.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					double price=ticketManager.getTotalPrice();
					
					Intent intent = new Intent();
					Bundle mBundle=new Bundle();
					mBundle.putDouble(PDAPaymentSelect.IN_TOTALS, price);
					intent.putExtras(mBundle);
			    	intent.setClass(TicketSaleTableActivity.this, PDAPaymentSelect.class);
			    	startActivityForResult(intent,PAYMENT_TICKET);
				}
			});	
	}
	
    protected void refushTicket(){
    	lineAdapter.clear();
    	Ticket currentTicket = ticketManager.getCurrentTicket();
    	for(TicketLine info:currentTicket.getLines())
    		lineAdapter.add(info);
    	ticketList.getListview().setSelection(currentTicket.getLines().size());
		refushState();
    }
    
    protected void refushState(){
    	btnTicketTitle.setText(TicketHelper.getName(ticketManager.getCurrentTicket()));
    	
    }
    
   
    
}